<?php
session_start();
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    header('Location: index.php');
    exit;
}

require_once __DIR__ . '/../core/config.php';
$db = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';charset=' . DB_CHARSET, DB_USER, DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// 获取当前页面
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$type = $_GET['type'] ?? 'action';
$limit = 50;
$offset = ($page - 1) * $limit;

try {
    // 获取日志数据
    if ($type === 'action') {
        $stmt = $db->prepare("SELECT * FROM action_logs ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $stmt->bindValue(1, $limit, PDO::PARAM_INT);
        $stmt->bindValue(2, $offset, PDO::PARAM_INT);
        $stmt->execute();
        $logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // 获取总数
        $stmt = $db->query("SELECT COUNT(*) as total FROM action_logs");
        $total = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
    } else {
        $stmt = $db->prepare("SELECT * FROM access_logs ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $stmt->bindValue(1, $limit, PDO::PARAM_INT);
        $stmt->bindValue(2, $offset, PDO::PARAM_INT);
        $stmt->execute();
        $logs = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        // 获取总数
        $stmt = $db->query("SELECT COUNT(*) as total FROM access_logs");
        $total = $stmt->fetch(PDO::FETCH_ASSOC)['total'];
    }
    $totalPages = ceil($total / $limit);
} catch (PDOException $e) {
    echo '<div class="bg-red-100 text-red-700 p-4 rounded mb-4">SQL错误：' . htmlspecialchars($e->getMessage()) . '</div>';
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>操作日志 - 幻影工具网</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#6366F1'
                    }
                }
            }
        }
    </script>
</head>
<body class="bg-gray-50 min-h-screen">
    <!-- 顶部导航 -->
    <nav class="bg-white shadow-sm border-b border-gray-200 sticky top-0 z-10">
        <div class="max-w-7xl mx-auto px-4 flex justify-between items-center h-16">
            <h1 class="text-xl font-bold text-blue-700">幻影工具网后台</h1>
            <div class="flex items-center space-x-4">
                <a href="/" class="text-gray-600 hover:text-blue-700">返回前台</a>
                <a href="logout.php" class="text-red-600 hover:text-red-800">退出登录</a>
            </div>
        </div>
    </nav>
    <div class="flex min-h-screen">
        <!-- 侧边栏 -->
        <aside class="w-64 bg-white shadow-md hidden md:block">
            <nav class="mt-8 space-y-2 px-4">
                <a href="index.php" class="block px-4 py-2 rounded-lg text-gray-600 hover:bg-blue-50 hover:text-blue-700">仪表盘</a>
                <a href="plugins.php" class="block px-4 py-2 rounded-lg text-gray-600 hover:bg-blue-50 hover:text-blue-700">插件管理</a>
                <a href="categories.php" class="block px-4 py-2 rounded-lg text-gray-600 hover:bg-blue-50 hover:text-blue-700">分类管理</a>
                <a href="stats.php" class="block px-4 py-2 rounded-lg text-gray-600 hover:bg-blue-50 hover:text-blue-700">数据统计</a>
                <a href="logs.php" class="block px-4 py-2 rounded-lg bg-blue-50 text-blue-700 font-bold">操作日志</a>
            </nav>
        </aside>
        <!-- 主内容区 -->
        <main class="flex-1 p-6">
            <div class="mb-8">
                <h2 class="text-2xl font-bold text-gray-900 mb-2">操作日志</h2>
                <p class="text-gray-500">查看系统操作日志和访问记录</p>
            </div>
            <!-- 日志类型切换 -->
            <div class="mb-6 flex gap-4">
                <a href="?type=action" class="px-4 py-2 rounded-lg <?php echo $type === 'action' ? 'bg-blue-600 text-white' : 'bg-white text-gray-700 border border-gray-300 hover:bg-gray-50'; ?>">操作日志</a>
                <a href="?type=access" class="px-4 py-2 rounded-lg <?php echo $type === 'access' ? 'bg-blue-600 text-white' : 'bg-white text-gray-700 border border-gray-300 hover:bg-gray-50'; ?>">访问日志</a>
            </div>
            <!-- 日志列表 -->
            <div class="bg-white rounded-xl shadow p-4 overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gray-50">
                        <tr>
                            <?php if ($type === 'action'): ?>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">时间</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">用户</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">操作</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">详情</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">IP地址</th>
                            <?php else: ?>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">时间</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">页面</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">IP地址</th>
                                <th class="px-6 py-3 text-left text-xs font-bold text-gray-500 uppercase">用户代理</th>
                            <?php endif; ?>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-100">
                        <?php foreach ($logs as $log): ?>
                        <tr>
                            <?php if ($type === 'action'): ?>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('Y-m-d H:i:s', strtotime($log['created_at'])); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900"><?php echo htmlspecialchars($log['user']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium 
                                        <?php 
                                        switch ($log['action']) {
                                            case 'login': echo 'bg-green-100 text-green-800'; break;
                                            case 'logout': echo 'bg-red-100 text-red-800'; break;
                                            case 'create': echo 'bg-blue-100 text-blue-800'; break;
                                            case 'update': echo 'bg-yellow-100 text-yellow-800'; break;
                                            case 'delete': echo 'bg-red-100 text-red-800'; break;
                                            default: echo 'bg-gray-100 text-gray-800';
                                        }
                                        ?>">
                                        <?php 
                                        switch ($log['action']) {
                                            case 'login': echo '登录'; break;
                                            case 'logout': echo '退出'; break;
                                            case 'create': echo '创建'; break;
                                            case 'update': echo '更新'; break;
                                            case 'delete': echo '删除'; break;
                                            default: echo htmlspecialchars($log['action']);
                                        }
                                        ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-500"><?php echo htmlspecialchars($log['details']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars($log['ip_address']); ?></td>
                            <?php else: ?>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo date('Y-m-d H:i:s', strtotime($log['created_at'])); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900"><?php echo htmlspecialchars($log['page']); ?></td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php echo htmlspecialchars($log['ip_address']); ?></td>
                                <td class="px-6 py-4 text-sm text-gray-500 max-w-xs truncate"><?php echo htmlspecialchars($log['user_agent']); ?></td>
                            <?php endif; ?>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <!-- 分页 -->
            <?php if ($totalPages > 1): ?>
            <div class="mt-6 flex items-center justify-between">
                <div class="text-sm text-gray-700">
                    显示第 <?php echo $offset + 1; ?> 到 <?php echo min($offset + $limit, $total); ?> 条，共 <?php echo $total; ?> 条记录
                </div>
                <div class="flex space-x-2">
                    <?php if ($page > 1): ?>
                        <a href="?type=<?php echo $type; ?>&page=<?php echo $page - 1; ?>" class="px-3 py-2 text-sm text-gray-500 bg-white border border-gray-300 rounded-md hover:bg-gray-50">上一页</a>
                    <?php endif; ?>
                    <?php for ($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
                        <a href="?type=<?php echo $type; ?>&page=<?php echo $i; ?>" class="px-3 py-2 text-sm <?php echo $i === $page ? 'text-blue-600 bg-blue-50 border border-blue-300' : 'text-gray-500 bg-white border border-gray-300 hover:bg-gray-50'; ?> rounded-md"><?php echo $i; ?></a>
                    <?php endfor; ?>
                    <?php if ($page < $totalPages): ?>
                        <a href="?type=<?php echo $type; ?>&page=<?php echo $page + 1; ?>" class="px-3 py-2 text-sm text-gray-500 bg-white border border-gray-300 rounded-md hover:bg-gray-50">下一页</a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
        </main>
    </div>
</body>
</html> 